package org.jdesktop.swingx.table.renderer;

import org.jdesktop.swingx.renderer.CellContext;
import org.jdesktop.swingx.renderer.ComponentProvider;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: babitsky.viacheslav
 * Date: 04.03.2008
 * Time: 14:09:45
 */
public class MultiLineTableRenderer extends DefaultTableRenderer {
    private static final long serialVersionUID = 7075374521957594819L;

    /**
     * Instantiates a default table renderer with the default component
     * provider.
     *
     * @see #DefaultTableRenderer(org.jdesktop.swingx.renderer.ComponentProvider)
     */
    public MultiLineTableRenderer() {
        super(new TextAreaProvider());
    }

    // -------------- implements javax.swing.table.TableCellRenderer
    /**
     * Returns a configured component, appropriate to render the given
     * list cell.
     *
     * @param table      the <code>JTable</code>
     * @param value      the value to assign to the cell at
     *                   <code>[row, column]</code>
     * @param isSelected true if cell is selected
     * @param hasFocus   true if cell has focus
     * @param row        the row of the cell to render
     * @param column     the column of the cell to render
     * @return the default table cell renderer
     */
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //cellContext.setWidth(table.getColumnModel().getColumn(column).getWidth());
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

class TextAreaProvider extends ComponentProvider<JTextArea> {
    private static final long serialVersionUID = 686670093963196926L;


    /**
     * Formats the renderering component's content from the
     * given cell context.
     *
     * @param context the cell context to configure from, must not be null.
     */
    protected void format(CellContext context) {
//        final TableCellContext tableCellContext = (TableCellContext) context;
//        rendererComponent.setSize(tableCellContext.getWidth(), 10);
//        int width = ((JTable) context.getComponent()).getColumnModel().getColumn(context.getColumn()).getWidth();
//        rendererComponent.setSize(width, 10);
//        Rectangle cellRect = ((JTable) context.getComponent()).getCellRect(context.getRow(), context.getColumn(), false);
//        rendererComponent.setSize(cellRect.width, cellRect.height);
        rendererComponent.setText((String) context.getValue());
//        Rectangle rect = table.getCellRect(row, column, false);
//        setSize(rect.width, rect.height);
//        setFont(table.getFont());

    }

    /**
     * Configures the renderering component's state from the
     * given cell context.
     *
     * @param context the cell context to configure from, must not be null.
     */
    protected void configureState(CellContext context) {
    }

    /**
     * Factory method to create and return the component to use for rendering.<p>
     *
     * @return the component to use for rendering.
     */
    protected JTextArea createRendererComponent() {
        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setColumns(70);

        return textArea;
    }

}
